import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import '../utils/request.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:provider/provider.dart';
import '../provider/playAudioList.dart';

class Search extends StatefulWidget {
  Search({Key? key}) : super(key: key);

  @override
  _SearchState createState() => _SearchState();
}

class _SearchState extends State<Search> {
  String _val = '';
  var _songs = [];
  void _subSearch() async {
    String val = this._val.trim();
    EasyLoading.show(status: 'loading...');
    var res = await getHttp('/search?keywords=$val'); //歌手部分信息和热门歌曲
    EasyLoading.dismiss();
    this.setState(() {
      _songs = res['result']['songs'];
      _val = '';
    });
  }

  @override
  Widget build(BuildContext context) {
    var playAudioList = Provider.of<PlayAudioList>(context);
    return Scaffold(
      appBar: AppBar(
        title: Row(
          children: [
            Container(
              width: 440.w,
              height: 80.w,
              padding: EdgeInsets.only(left: 3, top: 0),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Colors.blue[300],
              ),
              child: TextField(
                autofocus: true,
                maxLines: 1,
                style: TextStyle(
                  height: 2.5,
                  fontSize: 30.w,
                ),
                decoration: InputDecoration(
                  hintText: "请输入关键词",
                  hintMaxLines: 1,
                  hintStyle: TextStyle(
                    height: 3,
                    fontSize: 30.w,
                  ),
                  border: OutlineInputBorder(
                    borderSide: BorderSide.none,
                  ),
                ),
                onChanged: (str) {
                  setState(() {
                    _val = str;
                  });
                },
              ),
            ),
          ],
        ),
        actions: [
          Container(
            height: 60.w,
            width: 120.w,
            child: TextButton(
              style: ButtonStyle(
                alignment: Alignment.centerLeft,
              ),
              onPressed: _subSearch,
              child: Text(
                "搜索",
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
        ],
      ),
      body: ListView.separated(
        itemCount: _songs.length == 0 ? 1 : _songs.length,
        separatorBuilder: (BuildContext context, int index) {
          return Divider(height: 1, color: Colors.grey);
        },
        itemBuilder: (BuildContext context, int index) {
          return _songs.length == 0
              ? Container(
                  height: 200.w,
                  child: Center(
                    child: Text("请输入关键词搜索..."),
                  ),
                )
              : ListTile(
                  onTap: () {
                    var newList = [
                      {
                        "id": _songs[index]['id'],
                        "name": _songs[index]['name'],
                        "picUrl": _songs[index]['artists'][0]['img1v1Url']
                      }
                    ];
                    playAudioList.setCurrentIndex(0);
                    playAudioList.setAudioList(newList);
                    Navigator.pushNamed(context, "/audioplayer", arguments: {});
                  },
                  title: Text(_songs[index]['name']),
                  trailing: Icon(Icons.play_circle_fill_outlined),
                );
        },
      ),
    );
  }
}
